import 'package:jump_scape/js_data/js_box/js_member.dart';
import 'package:objectbox/objectbox.dart';

@Entity()
class GameRecordBox {
  @Id()
  int id = 0;

  String userId;
  int time;
  String gameType;

  @Property(type: PropertyType.date)
  DateTime createDate;

  @Transient()
  MemberBox? member;
  @Transient()
  String createDateString = '';

  GameRecordBox({
    required this.id,
    required this.userId,
    required this.time,
    required this.createDate,
    required this.gameType,
  });

  GameRecordBox.create({
    required this.userId,
    required this.time,
    required this.gameType,
  }) : createDate = DateTime.now();

  String formatCreateDate() {
    if (createDateString.isNotEmpty) {
      return createDateString;
    }

    DateTime dateTime = createDate;
    final now = DateTime.now();
    final today = DateTime(now.year, now.month, now.day);

    if (dateTime.isAfter(today) && dateTime.isBefore(today.add(Duration(days: 1)))) {
      // 如果是今天
      createDateString = '${dateTime.hour}:${dateTime.minute}';
    } else if (dateTime.year == now.year) {
      // 如果是今年但不是今天
      createDateString = '${dateTime.month}.${dateTime.day} '
          '${dateTime.hour.toString().padLeft(2, '0')}:${dateTime.minute.toString().padLeft(2, '0')}';
    } else {
      // 如果不是今年
      createDateString = '${dateTime.year}.${dateTime.month.toString().padLeft(2, '0')}.${dateTime.day.toString().padLeft(2, '0')} '
          '${dateTime.hour.toString().padLeft(2, '0')}:${dateTime.minute.toString().padLeft(2, '0')}';
    }

    return createDateString;
  }

  @override
  String toString() {
    return {
      'id': id,
      'userId': userId,
      'time': time,
      'createDate': createDate,
    }.toString();
  }
}
